package com.lct.simpleforecast.ui.adapter

import android.view.View
import com.lct.simpleforecast.R
import com.lct.simpleforecast.bean.ForecastBean
import com.lct.simpleforecast.extensions.loadImgByGlide
import com.lct.simpleforecast.extensions.setTextWithFormatArgs
import com.lct.simpleforecast.ui.adapter.base.MyBaseAdapter
import kotlinx.android.synthetic.main.rv_forecast_item.view.*
import java.text.SimpleDateFormat
import java.util.*


/**
 * Author：SkySmile
 * Date：2018/7/13
 * Description：
 */
class ForecastAdapter(layoutId: Int,
                      dailyForecastList: List<ForecastBean.HeWeather.DailyForecast>,
                      private val itemClick: (ForecastBean.HeWeather.DailyForecast) -> Unit) :
        MyBaseAdapter<ForecastBean.HeWeather.DailyForecast>(layoutId, dailyForecastList) {
    /**
     * 绑定数据
     *
     * @param   itemView item布局
     * @param   dataBean 数据对象
     */
    override fun bindData(itemView: View, dataBean: ForecastBean.HeWeather.DailyForecast) {
        with(dataBean) {
            itemView.ivIcon.loadImgByGlide(iconUrl())
            when (date) {
                getDate(0) -> itemView.tvDate.setText(R.string.today)
                getDate(1) -> itemView.tvDate.setText(R.string.tomorrow)
                else -> itemView.tvDate.text = date
            }
            itemView.tvDes.setTextWithFormatArgs(R.string.weather_des, cond_txt_d, cond_txt_n)
            itemView.tvTemp.setTextWithFormatArgs(R.string.weather_temp, tmp_max, tmp_min)
            itemView.setOnClickListener { itemClick(this) }
        }
    }

    /**
     * 获取格式化后的日期
     *
     * @param which 0表示今天，-1表示昨天，1表示明天，整数往后推,负数往前移动
     */
    private fun getDate(which: Int): String {
        var date = Date()
        val calendar = GregorianCalendar()
        calendar.time = date
        //把日期往后增加一天.整数往后推,负数往前移动,0表示当天
        calendar.add(Calendar.DATE, which)
        date = calendar.time
        return SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(date)
    }
}